package com.csx.micrometer.invoke.context.servlet;

import com.csx.micrometer.invoke.context.MicrometerMessage;
import com.csx.micrometer.invoke.context.MicrometerRequest;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @author cuisongxu
 * @date 2022/12/3 18:01
 */
public class MicrometerHttpServletRequest extends MicrometerRequest {

    public MicrometerHttpServletRequest(HttpServletRequest originRequest) {
        super(originRequest);
    }

    @Override
    public List<String> getHeaders(String name) {
        Enumeration<String> headerEnum = getOriginRequest().getHeaders(name);
        if(null == headerEnum) {
            return null;
        }
        LinkedList<String> headers = new LinkedList<>();
        while(headerEnum.hasMoreElements()) {
            String header = headerEnum.nextElement();
            headers.add(header);
        }

        return headers;
    }

    @Override
    public Map<String, String> getSingleValueHeaders() {
        Enumeration<String> headerNames = getOriginRequest().getHeaderNames();
        Map<String, String> headers = new HashMap<>();
        while (headerNames.hasMoreElements()) {
            String headerName = headerNames.nextElement();
            headers.put(headerName, getOriginRequest().getHeader(headerName));
        }
        headers.putAll(this.headers);
        return headers;
    }

    @Override
    public String getFirstHeader(String name) {
        return getOriginRequest().getHeader(name);
    }

    @Override
    public HttpServletRequest getOriginRequest() {
        return (HttpServletRequest) super.getOriginRequest();
    }

    @Override
    public Object getAttribute(String key) {
        return Optional.ofNullable(super.getAttribute(key))
                .orElse(getOriginRequest().getAttribute(key));
    }

    @Override
    public MicrometerMessage setAttribute(String key, Object value) {
        getOriginRequest().setAttribute(key, value);
        super.setAttribute(key, value);
        return this;
    }

    @Override
    public void removeAttribute(String key) {
        HttpServletRequest originRequest = getOriginRequest();
        originRequest.removeAttribute(key);
        super.removeAttribute(key);
    }

    @Override
    public Map<String, Object> getAttributes() {
        HttpServletRequest originRequest = getOriginRequest();
        Enumeration<String> attributeNames = originRequest.getAttributeNames();
        Map<String, Object> result = new HashMap<>();
        while(attributeNames.hasMoreElements()) {
            String key = attributeNames.nextElement();
            Object attribute = originRequest.getAttribute(key);
            result.put(key, attribute);
        }
        return result;
    }

    @Override
    public String getRequestURL() {
        return getOriginRequest().getRequestURL().toString();
    }

    @Override
    public String getRequestURI() {
        return getOriginRequest().getRequestURI();
    }

    @Override
    public String getMethod() {
        return getOriginRequest().getMethod();
    }

}
